package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.heima.article.entity.ApCollection;
import com.heima.article.mapper.ApCollectionMapper;
import com.heima.article.service.IApCollectionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.constants.message.HotArticleConstants;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.exception.LeadException;
import com.heima.common.threadlocal.UserThreadLocalUtils;
import com.heima.common.util.JsonUtils;
import com.heima.model.article.msg.ArticleBehaviorMsg;
import com.heima.model.behavior.dtos.CollectionBehaviorDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

import javax.management.Query;
import java.util.Date;

/**
 * <p>
 * APP收藏信息表 服务实现类
 * </p>
 *
 * @author heima
 * @since 2021-03-23
 */
@Service
public class ApCollectionServiceImpl extends ServiceImpl<ApCollectionMapper, ApCollection> implements IApCollectionService {

    @Autowired
    private KafkaTemplate kafkaTemplate;

    @Override
    public void saveCollectionBehavior(CollectionBehaviorDto dto) {
        Boolean isUser=false;
        //1、获取用户id，判断是用户操作还是设备操作
        Integer userId = UserThreadLocalUtils.getUserId();
        if(userId!=null && userId!=0){
            //用户登录，是用户的操作
            isUser=true;
        }
        //2、参数校验
        if(userId==null && dto.getEquipmentId()==null){
            throw new LeadException(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //3、查询用户或者设备是否收藏过该文章
        QueryWrapper<ApCollection> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ApCollection::getArticleId,dto.getArticleId());
        if(isUser){
            queryWrapper.lambda().eq(ApCollection::getApUserId,userId);
        }else {
            queryWrapper.lambda().eq(ApCollection::getEquipmentId,dto.getEquipmentId());
        }
        //执行查询
        ApCollection apCollection = this.getOne(queryWrapper);
        //判断用户是收藏还是取消收藏
        if(dto.getOperation()==1){
            //收藏操作
            if(apCollection==null){
                //保存收藏记录
                apCollection = new ApCollection();
                if(isUser){
                    apCollection.setApUserId(userId);
                }else {
                    apCollection.setEquipmentId(dto.getEquipmentId());
                }
                apCollection.setArticleId(dto.getArticleId());
                apCollection.setType(0);
                apCollection.setCollectionTime(new Date());
                apCollection.setPublishedTime(dto.getPublishedTime());
                this.save(apCollection);
                //生产收藏的行为消息
                ArticleBehaviorMsg articleBehaviorMsg = new ArticleBehaviorMsg(
                        dto.getArticleId(),
                        1,
                        ArticleBehaviorMsg.ArticleBehaviorType.COLLECTION
                );
                kafkaTemplate.send(HotArticleConstants.HOT_ARTICLE_SCORE_STREAM_TOPIC, JsonUtils.toString(articleBehaviorMsg));
            }
        }else{
            //取消收藏，删除收藏记录
            this.remove(queryWrapper);
        }

    }
}
